package com.library.repository;

import com.library.entity.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * 通知数据访问层接口
 */
@Repository
public interface NotificationRepository extends JpaRepository<Notification, Long> {
    /**
     * 根据用户ID查找通知列表，按创建时间倒序
     */
    @Query("SELECT n FROM Notification n JOIN FETCH n.user " +
           "WHERE n.user.id = :userId ORDER BY n.createdAt DESC")
    List<Notification> findByUserIdOrderByCreatedAtDesc(@Param("userId") Long userId);

    /**
     * 根据用户ID和是否已读查找通知列表
     */
    @Query("SELECT n FROM Notification n JOIN FETCH n.user " +
           "WHERE n.user.id = :userId AND n.isRead = :isRead ORDER BY n.createdAt DESC")
    List<Notification> findByUserIdAndIsReadOrderByCreatedAtDesc(@Param("userId") Long userId, @Param("isRead") Boolean isRead);

    /**
     * 统计用户未读通知数量
     */
    long countByUserIdAndIsReadFalse(Long userId);

    /**
     * 根据用户ID删除通知
     *
     * @param userId 用户ID
     */
    void deleteByUserId(Long userId);
}

